<template>
  <div class="app-container">
    <el-card>
      <div class="filter-container">
        <el-select v-model="listQuery.type" placeholder="活动类型" clearable style="width: 120px">
          <el-option label="考试" :value="1" />
          <el-option label="测评" :value="2" />
          <el-option label="作业" :value="3" />
        </el-select>

        <el-input
          v-model="listQuery.searchKey"
          placeholder="搜索考试名称"
          style="width: 200px; margin-left: 10px"
          clearable
          @keyup.enter.native="handleFilter"
        />

        <el-button
          type="primary"
          style="margin-left: 10px"
          @click="handleFilter"
        >
          搜索
        </el-button>
      </div>

      <el-table
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%; margin-top: 20px"
      >
        <el-table-column label="考试/测评" prop="title" min-width="200" />
        <el-table-column label="类型" width="100">
          <template slot-scope="{row}">
            <el-tag :type="row.type | typeFilter">
              {{ row.type | typeNameFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" prop="endTime" width="180">
          <template slot-scope="{row}">
            {{ row.endTime | parseTime('{y}-{m}-{d} {h}:{i}') }}
          </template>
        </el-table-column>
        <el-table-column label="参与人数" prop="participantCount" width="100" />
        <el-table-column label="平均分" prop="avgScore" width="100" />
        <el-table-column label="通过率" prop="passRate" width="100">
          <template slot-scope="{row}">
            {{ row.passRate }}%
          </template>
        </el-table-column>
        <el-table-column label="最高分" prop="highestScore" width="100" />
        <el-table-column label="最低分" prop="lowestScore" width="100" />
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="{row}">
            <el-button
              type="primary"
              size="mini"
              @click="handleDetail(row.id)"
            >
              详情
            </el-button>
            <el-button
              v-if="row.scorePubedStatus === 0"
              type="success"
              size="mini"
              @click="handlePublish(row.id)"
            >
              发布
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { fetchScoreList, publishScore } from '@/api/exam/score'
import Pagination from '@/components/Pagination/index.vue'

export default {
  name: 'ScoreList',
  components: { Pagination },
  filters: {
    typeFilter(type) {
      const typeMap = {
        1: 'danger',
        2: 'success',
        3: 'info'
      }
      return typeMap[type]
    },
    typeNameFilter(type) {
      const typeNameMap = {
        1: '考试',
        2: '测评',
        3: '作业'
      }
      return typeNameMap[type]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        groupId: undefined,
        type: undefined,
        searchKey: undefined
      }
    }
  },
  created() {
    this.listQuery.groupId = this.$route.query.groupId
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchScoreList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDetail(id) {
      this.$router.push({
        path: '/score/detail',
        query: { examId: id, groupId: this.listQuery.groupId }
      })
    },
    handlePublish(id) {
      this.$confirm('确定要发布成绩吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        publishScore(id).then(() => {
          this.$message({
            type: 'success',
            message: '发布成功!'
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>
.filter-container {
  margin-bottom: 20px;
}
</style>
